package com.china08.yxyapi.service.yxyresource.statistics.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.service.yxyresource.statistics.StatisticsEachWeikeService;
@Service
public class StatisticsEachWeikeServiceImpl implements StatisticsEachWeikeService{

	@Autowired
	private ElasticsearchTemplate et;
	/**
	 * 根据学段进行分组
	 */
	@Override
	public Map<String, Object> statisticsResourceCount(String districtCode, Integer schoolBureauLevel) {
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType2 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder.must(builderType2);
		TermQueryBuilder builderType = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder.must(builderType);
		if (schoolBureauLevel == 0) {
			TermQueryBuilder builderDel = new TermQueryBuilder("districtCode", districtCode);
			boolQueryBuilder.must(builderDel);
		}else if(schoolBureauLevel == 1){
			TermQueryBuilder builderDel = new TermQueryBuilder("cityCode", districtCode);
			boolQueryBuilder.must(builderDel);
		}else if (schoolBureauLevel == 2) {
			TermQueryBuilder builderDel = new TermQueryBuilder("provinceCode", districtCode);
			boolQueryBuilder.must(builderDel);
		}
//        192.168.100.171:9300,192.168.100.172:9300,192.168.100.173:9300,192.168.100.174:9300
		SearchRequestBuilder searchRequestBuilder = et.getClient()  
                .prepareSearch("yxy_res").setTypes("resource"); 
		searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);  
		TermsBuilder gradeTermsBuilder = AggregationBuilders.terms("phaseIdAgg").field("stage").size(100);
		searchRequestBuilder.addAggregation(gradeTermsBuilder);
		SearchResponse sr = searchRequestBuilder.setQuery(boolQueryBuilder).execute().actionGet();
		Map<String, Aggregation> aggMap = sr.getAggregations().asMap();
		StringTerms gradeTerms = (StringTerms) aggMap.get("phaseIdAgg");
		Iterator<Bucket> gradeBucketIt = gradeTerms.getBuckets().iterator();
		
		//返回
		Map<String,Object> map=new HashMap<String,Object>();
	    while(gradeBucketIt.hasNext())
	    {
	         Bucket gradeBucket = gradeBucketIt.next();
	         String key = (String) gradeBucket.getKey();
	         long value= gradeBucket.getDocCount();
	         map.put(key, value);
	         System.out.println(key + "学段有" + value +"个资源。");
	         System.out.println();
	    }
		return map;
	}
}
